package core.september.cloudbadge.engine;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import core.september.cloudbadge.model.user.UserModel;
import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.io.HCardPage;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class RenderEngine {

	public static String renderHTML(UserModel model) {
		model.commit();
		VCard rend = model.getUf().getVcard();
		StringWriter writer = new StringWriter();
	    try {
			Ezvcard.writeHtml(rend).go(writer,getTemplate(model));
			return writer.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
		
	}
	//Ezvcard.writeJson(vcard).warnings(warnings).go(file);
	public static String renderJSON(UserModel model) {
		model.commit();
		VCard rend = model.getUf().getVcard();
		StringWriter writer = new StringWriter();
	    try {
			Ezvcard.writeJson(rend).warnings(new ArrayList<String>()).go(writer);
			return writer.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
		
	}
	
	public static String renderVCard(UserModel model) {
		model.commit();
		VCard rend = model.getUf().getVcard();
		StringWriter writer = new StringWriter();
	    try {
			Ezvcard.write(rend).warnings(new ArrayList<String>()).go(writer);
			return writer.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
		
	}
	
	private static Template getTemplate(UserModel model) {
			Configuration cfg = new Configuration();
			cfg.setClassForTemplateLoading(HCardPage.class, "");
			cfg.setObjectWrapper(new DefaultObjectWrapper());
			cfg.setWhitespaceStripping(true);
			try {
				if(model.getTemplate() == null || model.getTemplate().getContent() == null) {
					return  cfg.getTemplate("hcard-template.ftl");
				}
				else {
					return new Template("name", new StringReader( model.getTemplate().getContent().getValue()), cfg);
				}
				
				//template = new Template("name", new StringReader(templateStr), cfg);
			} catch (IOException e) {
				//should never be thrown because it's always on the classpath
				throw new RuntimeException(e);
			}
	}
	
	public static String compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        System.out.println("String length : " + str.length());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        String outStr = out.toString("ISO-8859-1");
        System.out.println("Output String lenght : " + outStr.length());
        return outStr;
     }
    
    public static String decompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        System.out.println("Input String length : " + str.length());
        GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("ISO-8859-1")));
        BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "ISO-8859-1"));
        String outStr = "";
        String line;
        while ((line=bf.readLine())!=null) {
          outStr += line;
        }
        System.out.println("Output String lenght : " + outStr.length());
        return outStr;
     }
}

